
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#include "RTMaterial.h"
#include "RTProcTextureChecker.h"
#include "RTScene.h"

namespace RT
{

	//constructor
	RTMaterial::RTMaterial(): /*m_proceduralTexture(0),*/ m_bumpMap(0), m_diffuseTexture(0){}

	//destructor
	RTMaterial::~RTMaterial()
	{
		/*if(m_proceduralTexture != 0)
		{
			delete m_proceduralTexture;
			m_proceduralTexture = 0;
		}*/

		if(m_bumpMap != 0)
		{
			delete m_bumpMap;
			m_bumpMap = 0;
		}
	}

	//set the ambient component using a color
	void RTMaterial::setAmbient(const RTColor& ambient)
	{
		_ambient.setColor(ambient);
		_ambient.setRTComponentSource(CS_COLOR);
	}

	//set the ambient component using a procedural texture
	void RTMaterial::setAmbient(RTIProcTexture* const procTexture)
	{
		_ambient.setProceduralTexture(procTexture);
		_ambient.setRTComponentSource(CS_PROC_TEXTURE);
	}

	//set the ambient component using a texture from a file
	void RTMaterial::setAmbient(RTTexture* const fromFileTexture)
	{
		_ambient.setFileTexture(fromFileTexture);
		_ambient.setRTComponentSource(CS_FILE_TEXTURE);
	}

	//set the diffuse component using a color
	void RTMaterial::setDiffuse(const RTColor& diffuse)
	{
		_diffuse.setColor(diffuse);
		_diffuse.setRTComponentSource(CS_COLOR);
	}

	//set the diffuse component using a procedural texture
	void RTMaterial::setDiffuse(RTIProcTexture* const procTexture)
	{
		_diffuse.setProceduralTexture(procTexture);
		_diffuse.setRTComponentSource(CS_PROC_TEXTURE);
	}

	//set the diffuse component using a file texture
	void RTMaterial::setDiffuse(RTTexture* const fromFileTexture)
	{
		_diffuse.setFileTexture(fromFileTexture);
		_diffuse.setRTComponentSource(CS_FILE_TEXTURE);
	}

	//set the specular component using a color
	void RTMaterial::setSpecular(const RTColor& specular)
	{
		_specular.setColor(specular);
		_specular.setRTComponentSource(CS_COLOR);
	}

	//set the specular component using a procedural texture
	void RTMaterial::setSpecular(RTIProcTexture* const procTexture)
	{
		_specular.setProceduralTexture(procTexture);
		_specular.setRTComponentSource(CS_PROC_TEXTURE);
	}

	//set the specular component using a file texture
	void RTMaterial::setSpecular(RTTexture* const fromFileTexture)
	{
		_specular.setFileTexture(fromFileTexture);
		_specular.setRTComponentSource(CS_FILE_TEXTURE);
	}

	//set the bump map
	void RTMaterial::setBumpMap(RTIProcTexture* bump)
	{
		m_bumpMap = bump;
	}

	//Return the ambient color of the component on the poI32 v in the RTPrimitive p.
	RTColor RTMaterial::getAmbient(const RTVector3f& v, const RTIPrimitive& p)const
	{
		return _ambient.getColor(v, p);
	}

	//Return the specular color of the component on the poI32 v in the RTPrimitive p.
	RTColor RTMaterial::getSpecular(const RTVector3f& v, const RTIPrimitive& p)const
	{
		return _specular.getColor(v, p);
	}

	//Return the diffuse color of the component on the poI32 v in the RTPrimitive p.
	RTColor RTMaterial::getDiffuse(const RTVector3f& v, const RTIPrimitive& p)const
	{
		return _diffuse.getColor(v, p);
	}

	//return the normal of the point using a bump mapping texture
	RTVector3f RTMaterial::getBumpNormal(const RTVector3f& v)const
	{
		if(m_bumpMap != 0)
			return m_bumpMap->getColor(v);


		return RTColor();
	}

	//Check if this material use a bump map
	bool RTMaterial::useBumpMapping()const
	{
		if(m_bumpMap == 0)
			return false;

		return true;
	}

}